<link href="asset/css/show.css" rel="stylesheet" type="text/css">
<script src="asset/js/cetak_excel.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#show").hide();
	$("#sembunyi").hide();
	$("#tampil").show();
	
	$("#tampil").click(function(){
		$("#show").show(500);
		$("#tampil").hide();
		$("#sembunyi").show();
	});
	$("#sembunyi").click(function(){
		$("#show").hide(500);
		$("#sembunyi").hide();
		$("#tampil").show();
	});
	
	//$(".show2").hide();
	//$(".kec").hide();
	pilih_data();
	
	$("#periode").change(function(){
		var periode = $("#periode").val();
		if(periode == 1){
			document.getElementById('tgl1').disabled = false;
			document.getElementById('tgl2').disabled = false;
			document.getElementById('bln1').disabled = false;
			document.getElementById('bln2').disabled = false;
		}
		else if(periode == 2){
			document.getElementById('tgl1').disabled = true;
			document.getElementById('tgl2').disabled = true;
			document.getElementById('bln1').disabled = false;
			document.getElementById('bln2').disabled = false;
		}else{
			document.getElementById('tgl1').disabled = true;
			document.getElementById('tgl2').disabled = true;
			document.getElementById('bln1').disabled = true;
			document.getElementById('bln2').disabled = true;
		}
	});
	
	$("#pilihan1").change(function(){
		var pil = $("#pilihan1").val();
		if(pil == 1){
			$(".kec").show(250);
		}else{
			$(".kec").hide();
		}
	});
	
	$("#pilihan3").change(function(){
		var pil = $("#pilihan3").val();
		if(pil == 1){
			$(".kec").show(250);
		}else{
			$(".kec").hide();
		}
	});
	
	$("#dimensi").change(function(){
		pilih_data();
	});
});
function pilih_data(){
	var dimensi = $("#dimensi").val();
	var pilihan1 = $("#pilihan1").val();
	var pilihan3 = $("#pilihan3").val();
		if(dimensi == 2){
			$(".show1").hide();
			$(".show2").show(250);
			if(pilihan3 == 1){
				$(".kec").show(250);
			}else{
				$(".kec").hide();
			}
		}else{
			$(".show1").show();
			$(".show2").hide(250);
			if(pilihan1 == 1){
				$(".kec").show(250);
			}else{
				$(".kec").hide();
			}
		}
}
</script>
<?php
	$tgl = date("d");
	$bln = date("m");
	$thn = date("Y");
	$dimensi = $_POST['dimensi'];
	if($dimensi=='2'){
		$status_show1='none';
		$status_show2='';
	}else{
		$status_show1='';
		$status_show2='none';
	}
	
	$pilihan1 = $_POST['pilihan1'];
	if($pilihan1=='1'){
		$status_kec='';
	}else if($pilihan3=='1'){
		$status_kec='';
	}else{
		$status_kec='none';
	}
	
	$periode = $_POST['periode'];
	$tgl1 = $_POST['tgl1'];
	$tgl2 = $_POST['tgl2'];
	$bln1 = $_POST['bln1'];
	$bln2 = $_POST['bln2'];
	$thn1 = $_POST['thn1'];
	$thn2 = $_POST['thn2'];
	
	$pilihan1 = $_POST['pilihan1'];
	$pilihan2 = $_POST['pilihan2'];
	$pilihan3 = $_POST['pilihan3'];
	
	$kecamatan = $_POST['kecamatan'];
?>
<div id="tiga_ba">
	<div id="tiga_baa">Laporan Statistik Peserta </div>
	<div id="tiga_bab">
		
	</div>
	<div class="clear"></div>
</div>
	<form method="post" action='utama.php?module=mod_statistik&view=peserta'>
	<table style="margin-left: 5px;">
		<tr>
			<td width="75">Periode </td>
			<td width="300">
				<select name="periode" id="periode" class="input1">
					<?php
						if($periode == 1){
							$s1 = "selected";
						}else if($periode == 2){
							$p1 = "disabled";
							$s2 = "selected";
						}else{
							$p1 = "disabled";
							$p2 = "disabled";
							$s3 = "selected";
						}
					?>
					<option value="1" <?php echo $s1; ?>>Hari</option>
					<option value="2" <?php echo $s2; ?>>Bulan</option>
					<option value="3" <?php echo $s3; ?>>Tahun</option>
				</select>
			</td>
			<td width="100">Dimensi</td>
			<td width="300">
			<?php
			$arr_dimensi=array('1'=>'1 Dimensi','2'=>'2 Dimensi');
			kombo_from_array("dimensi","input2",$arr_dimensi,$dimensi);
				?>
				
			</td>
		</tr>
		<tr>
			<td>Dari </td>
			<td>
				<select name="tgl1" id="tgl1" class="input1" <?php echo $p1; ?>>
					<option value="">Tgl</option>
					<?php
						for($i=1;$i<=31;$i++){
							if($i == $tgl && $tgl1 == ''){
								$s = "selected";
							}else if($i == $tgl1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="bln1" id="bln1" class="input1" <?php echo $p2; ?>>
					<option value="">Bln</option>
					<?php
						for($i=1;$i<=12;$i++){
							if($i == $bln && $bln1 == ''){
								$s = "selected";
							}else if($i == $bln1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="thn1" id="thn1" class="input1">
					<option value="">Thn</option>
					<?php
						for($i=2010;$i<=2020;$i++){
							if($i == $thn && $thn1 == ''){
								$s = "selected";
							}else if($i == $thn1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
			</td>
			<td>
				<span style="display:<?php echo $status_show1;?>" class="show1">
					Baris
				</span>
				<span style="display:<?php echo $status_show2;?>" class="show2">
					Kolom
				</span>
			</td>
			<td>
				<span style="display:<?php echo $status_show2;?>" class="show2">
				<?php
				$arr_jenis=array('3'=>'Umur','4'=>'Jenis Peserta','5'=>'Agama','6'=>'Jenis Kelamin');
			kombo_from_array("pilihan2","input2",$arr_jenis,$pilihan2);
				?>
				</span>
				<span style="display:<?php echo $status_show1;?>" class="show1">
				<?php
			$arr_jenis=array('1'=>'Desa','2'=>'Kecamatan','3'=>'Umur','4'=>'Jenis Peserta','5'=>'Agama','6'=>'Jenis Kelamin');
			kombo_from_array("pilihan1","input2",$arr_jenis,$pilihan1);
				?>	
				</span>
			</td>
		</tr>
		<tr>
			<td>Sampai </td>
			<td>
				<select name="tgl2" id="tgl2" class="input1" <?php echo $p1; ?>>
					<option value="">Tgl</option>
					<?php
						for($i=1;$i<=31;$i++){
							if($i == $tgl && $tgl2 == ''){
								$s = "selected";
							}else if($i == $tgl2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="bln2" id="bln2" class="input1" <?php echo $p2; ?>>
					<option value="">Bln</option>
					<?php
						for($i=1;$i<=12;$i++){
							if($i == $bln && $bln2 == ''){
								$s = "selected";
							}else if($i == $bln2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="thn2" id="thn2" class="input1">
					<option value="">Thn</option>
					<?php
						for($i=2010;$i<=2020;$i++){
							if($i == $thn && $thn2 == ''){
								$s = "selected";
							}else if($i == $thn2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
			</td>
			<td valign="top" style="padding-top: 10px;">
				<span style="display:<?php echo $status_show2;?>" class="show2">Baris</span>
			</td>
			<td>
				<span style="display:<?php echo $status_show2;?>" class="show2">
				<?php
					$arr_jenis=array('1'=>'Desa','2'=>'Kecamatan','3'=>'Umur','4'=>'Jenis Peserta','5'=>'Agama','6'=>'Jenis Kelamin');
					kombo_from_array("pilihan3","input2",$arr_jenis,$pilihan3);
				?>	
				</span>
				<span style="display:<?php echo $status_kec;?>" class="kec">
					<?php
						kombo_kec("kecamatan","input2","kecamatan","$kecamatan");
					?>
				</span>
			</td>
		</tr>
		<tr>
			<td></td>
			<td><input name="submit" id="proses" type="submit" class="button" value="Proses" /></td>
			<td></td>
			<td></td>
		</tr>
	</table>
</form>
<?php
	if(isset($_POST['submit'])){
?>
	<div id="btnChart">
		<span id="tampil"><img src="asset/images/chart.png" title="Tampilkan Grafik" width="30"></span>
		<span id="sembunyi"><img src="asset/images/chart2.png" title="Sembunyikan Grafik" width="30"></span>
		<span id="print">
			<a href="#" class="export">
				<img src="asset/images/print.png" title="Cetak" width="30">
			</a>
		</span>
	</div>
	<div id="show">
		<script type="text/javascript">
			$(function () {
				$('#container').highcharts({
					data: {
						table: document.getElementById('datatable')
					},
					chart: {
						type: 'column'
					},
					title: {
						text: 'Grafik Data Peserta'
					},
					yAxis: {
						allowDecimals: false,
						title: {
							text: 'Jumlah Peserta'
						}
					},
					tooltip: {
						formatter: function() {
							return '<b>'+ this.series.val +'</b><br/>';
								//this.y +' '+ this.x.toLowerCase();
						}
					}
				});
			});
		</script>
		<script src="chart/highcharts.js"></script>
		<script src="chart/modules/data.js"></script>
		<script src="chart/modules/exporting.js"></script>
		<div id="container" style="width: 790px; height: 400px; margin: 0 auto"></div>
	</div>
<?php
	}
?>
			
<?php
	if($periode == 1){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-$tgl1'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-$tgl2'";
	}else if($periode == 2){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-31'";
	}else{
		$tgl1 = "and p.tgl_mulai >= '$thn1-01-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-12-31'";
	}
	
	if($dimensi == 1){
		if($pilihan1 == 1){
			$sql="select d.id_desa, d.desa,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk where k.id_desa = d.id_desa $tgl1 $tgl2) as jml from desa d inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten where kb.aktif = 'Y' and d.id_kecamatan = '$kecamatan' order by d.desa";
		}else if($pilihan1 == 2){
			$sql="select kc.id_kecamatan, kc.kecamatan,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk inner join desa d on k.id_desa = d.id_desa where d.id_kecamatan = kc.id_kecamatan $tgl1 $tgl2) as jml from kecamatan kc inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten where kb.aktif = 'Y' order by kc.kecamatan";
		}else if($pilihan1 == 3){
			$sql="SELECT um.id_umur, um.umur,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk inner join desa d on k.id_desa = d.id_desa inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten WHERE kb.aktif = 'Y' and DATEDIFF(tgl_mulai, pn.tgl_lahir) BETWEEN um.dari AND um.sampai $tgl1 $tgl2) AS jml FROM umur um ORDER BY um.id_umur";
		}else if($pilihan1 == 4){
			$sql="select jp.id_jenis_peserta, jp.jenis_peserta,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk inner join desa d on k.id_desa = d.id_desa inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten where p.id_jenis_peserta = jp.id_jenis_peserta $tgl1 $tgl2 and kb.aktif = 'Y') as jml from jenis_peserta jp order by jp.jenis_peserta";
		}else if($pilihan1 == 5){
			$sql="select a.id_agama, a.agama,(SELECT COUNT(id_peserta) FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk inner join desa d on k.id_desa = d.id_desa inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten where pn.id_agama = a.id_agama $tgl1 $tgl2 and kb.aktif = 'Y') as jml from agama a order by a.agama";
		}else if($pilihan1 == 6){
			$lk=mysql_fetch_array(mysql_query("SELECT COUNT(id_peserta) as jml FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk inner join desa d on k.id_desa = d.id_desa inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten where kb.aktif = 'Y' $tgl1 $tgl2 and pn.jk = 'L'"));
			$pr=mysql_fetch_array(mysql_query("SELECT COUNT(id_peserta) as jml FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk inner join desa d on k.id_desa = d.id_desa inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten where kb.aktif = 'Y' $tgl1 $tgl2 and pn.jk = 'P'"));
		}
		//echo $sql;
		if($_POST['submit'] == 'Proses'){
		
			if($pilihan1 != 6){
			?>
				<div id="dvData">
				<table class="tabel">
					<tr>
						<th>NO</th>
						<th>Nama</th>
						<th>Jumlah</th>
					</tr>
					<?php
						//echo $sql;
						$qry=mysql_query($sql);
							while($hasil=mysql_fetch_array($qry)){
								$no++;
					?>
								<tr>
									<td align="center" width="30"><?php echo $no;?></td>
									<td>
										<?php
											if($pilihan1 == 1){
												echo $hasil['desa'];
											}else if($pilihan1 == 2){
												echo $hasil['kecamatan'];
											}else if($pilihan1 == 3){
												echo $hasil['umur'];
											}else if($pilihan1 == 4){
												echo $hasil['jenis_peserta'];
											}else if($pilihan1 == 5){
												echo $hasil['agama'];
											}
										?>
									</td>
									<td align="center" align="center" width="60"><?php echo $hasil['jml'];?></td>
								</tr>
								
						<?php
								$jumlah = $jumlah + $hasil['jml'];
							}
						?>
					<tr>
						<td align="center" colspan="2"><b>Jumlah</b></td>
						<td align="center" align="center"><?php echo "<b>$jumlah</b>";?></td>
					</tr>
				</table>
				</div>
			<?php
			}else{
			?>
				<div id="dvData">
				<table class="tabel">
				<thead>
					<tr>
						<th>NO</th>
						<th>Nama</th>
						<th>Jumlah</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td align="center" width="30">1</td>
						<td><?php echo "Laki-laki"; ?></td>
						<td align="center" align="center" width="60"><?php echo $lk['jml'];?></td>
					</tr>
					<tr>
						<td align="center" width="30">2</td>
						<td><?php echo "Perempuan"; ?></td>
						<td align="center" align="center" width="120"><?php echo $pr['jml'];?></td>
					</tr>
					<tr>
						<td align="center" colspan="2"><b>Jumlah</b></td>
						<td align="center" align="center">
							<?php 
								$jumlah = $pr['jml'] + $lk['jml'];
								echo "<b>$jumlah</b>";
							?>
						</td>
					</tr>
				</tbody>
				</table>
				</div>
			<?php
			}
			
			//----------------- Tabel Hidden---------------------
			if($pilihan1 != 6){
			?>
				<table class="tabel" id="datatable" style="display: none;">
				<thead>
					<tr>
						<!--<th>NO</th>-->
						<th>Nama</th>
						<th>Peserta</th>
					</tr>
					</thead>
				<tbody>
					<?php
						//echo $sql;
						$qry=mysql_query($sql);
							while($hasil=mysql_fetch_array($qry)){
								$no++;
					?>
								<tr>
									<!--<td align="center" width="30"><?php echo $no;?></td>-->
									<td >
										<?php
											if($pilihan1 == 1){
												echo $hasil['desa'];
											}else if($pilihan1 == 2){
												echo $hasil['kecamatan'];
											}else if($pilihan1 == 3){
												echo $hasil['umur'];
											}else if($pilihan1 == 4){
												echo $hasil['jenis_peserta'];
											}else if($pilihan1 == 5){
												echo $hasil['agama'];
											}
										?>
									</td>
									<td align="center" align="center" width="120"><?php echo $hasil['jml'];?></td>
								</tr>
								
						<?php
								$jumlah = $jumlah + $hasil['jml'];
							}
						?>
					</tbody>
				</table>
			<?php
			}else{
			?>
				<table class="tabel" id="datatable" style="display: none;">
				<thead>
					<tr>
						<!--<th>NO</th>-->
						<th>Nama</th>
						<th>Jumlah Peserta</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<!--<td align="center" width="30">1</td>-->
						<td><?php echo "Laki-laki"; ?></td>
						<td align="center" align="center" width="60"><?php echo $lk['jml'];?></td>
					</tr>
					<tr>
						<!--<td align="center" width="30">2</td>-->
						<td><?php echo "Perempuan"; ?></td>
						<td align="center" align="center" width="120"><?php echo $pr['jml'];?></td>
					</tr>
					</tbody>
				</table>
			<?php
			}
			//-----------------------------------
		}
	}else if($dimensi == 2){
		//Buat kolom
		if($pilihan2 == 3){
			$sql_kolom="SELECT CONCAT(u.dari,'-',u.sampai) AS id,u.umur AS nama FROM umur u ORDER BY u.id_umur ASC";
		}else if($pilihan2 == 4){
			$sql_kolom="select id_jenis_peserta as id,jenis_peserta as nama from jenis_peserta order by id_jenis_peserta asc";
		}else if($pilihan2 == 5){
			$sql_kolom="select id_agama as id,agama as nama from agama order by id_agama asc";
		}else if($pilihan2 == 6){
			$sql_kolom="select id as id,nama as nama from jenis_kelamin order by id asc";
		}
		$arr_kolom=get_arr_query($sql_kolom);

		foreach($arr_kolom as $val){
			$arr_id_kolom[]=$val['id'];
			$arr_nama_kolom[]=$val['nama'];
		}
		foreach($arr_id_kolom as $key=>$val_data){
			$arr_label[]="label_".($key+1);
			$tmp='label_'.($key+1);
			if($pilihan2 == 3 && $pilihan3 == 1){				
				$pecah_tgl=explode("-",$val_data);
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN ".$pecah_tgl[0]." AND ".$pecah_tgl[1]." and k.id_desa=d.id_desa $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 3 && $pilihan3 == 2){				
				$pecah_tgl=explode("-",$val_data);
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN ".$pecah_tgl[0]." AND ".$pecah_tgl[1]." and kc.id_kecamatan=kcm.id_kecamatan $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 3 && $pilihan3 == 4){				
				$pecah_tgl=explode("-",$val_data);
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN ".$pecah_tgl[0]." AND ".$pecah_tgl[1]." and p.id_jenis_peserta=j.id_jenis_peserta and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 3 && $pilihan3 == 5){				
				$pecah_tgl=explode("-",$val_data);
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN ".$pecah_tgl[0]." AND ".$pecah_tgl[1]." and pn.id_agama=ag.id_agama and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 3 && $pilihan3 == 6){
				$pecah_tgl=explode("-",$val_data);
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN ".$pecah_tgl[0]." AND ".$pecah_tgl[1]." and pn.jk=jk.id and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 4 && $pilihan3 == 1){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where p.id_jenis_peserta='".$val_data."' and k.id_desa=d.id_desa and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 4 && $pilihan3 == 2){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where p.id_jenis_peserta='".$val_data."' and ds.id_kecamatan=kcm.id_kecamatan and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 4 && $pilihan3 == 3){				
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN um.dari AND um.sampai and p.id_jenis_peserta='".$val_data."' and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 4 && $pilihan3 == 4){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where p.id_jenis_peserta='".$val_data."' and p.id_jenis_peserta=j.id_jenis_peserta and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 4 && $pilihan3 == 5){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where p.id_jenis_peserta='".$val_data."' and pn.id_agama=ag.id_agama and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 4 && $pilihan3 == 6){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where p.id_jenis_peserta='".$val_data."' and pn.jk=jk.id and kc.id_kabupaten = '6405' and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 5 && $pilihan3 == 1){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.id_agama='".$val_data."' and k.id_desa=d.id_desa and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 5 && $pilihan3 == 2){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.id_agama='".$val_data."' and ds.id_kecamatan=kcm.id_kecamatan and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 5 && $pilihan3 == 3){				
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN um.dari AND um.sampai and pn.id_agama='".$val_data."' and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 5 && $pilihan3 == 4){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.id_agama='".$val_data."' and p.id_jenis_peserta=j.id_jenis_peserta and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 5 && $pilihan3 == 5){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.id_agama='".$val_data."' and pn.id_agama=ag.id_agama and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 5 && $pilihan3 == 6){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.jk='".$val_data."' and pn.jk=jk.id and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 6 && $pilihan3 == 1){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.jk='".$val_data."' and k.id_desa=d.id_desa and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 6 && $pilihan3 == 2){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.jk='".$val_data."' and ds.id_kecamatan=kcm.id_kecamatan and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 6 && $pilihan3 == 3){				
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where DATEDIFF(p.tgl_mulai, pn.tgl_lahir) BETWEEN um.dari AND um.sampai and pn.jk='".$val_data."' and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 6 && $pilihan3 == 4){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.id_agama='".$val_data."' and p.id_jenis_peserta=j.id_jenis_peserta and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 6 && $pilihan3 == 5){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.jk='".$val_data."' and pn.id_agama=ag.id_agama and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}else if($pilihan2 == 6 && $pilihan3 == 6){
				$sql_jumlah.="ifnull((select count(p.id_peserta) as jumlah FROM peserta p INNER JOIN penduduk pn ON p.id_penduduk = pn.id_penduduk inner join keluarga k on pn.id_kk = k.id_kk inner join desa ds on ds.id_desa = k.id_desa inner join kecamatan kc on ds.id_kecamatan = kc.id_kecamatan where pn.jk='".$val_data."' and pn.jk=jk.id and kc.id_kabupaten = '6405' $tgl1 $tgl2),0) as '$tmp',";
			}
			//echo $sql_jumlah;
		}
		//buat baris
		if($pilihan3 == 1){
			$sql_data="select d.id_desa as id,".$sql_jumlah." d.desa as nama from desa d inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten where kb.aktif = 'Y' and d.id_kecamatan = '$kecamatan' order by d.desa asc";
		}if($pilihan3 == 2){
			$sql_data="select kcm.id_kecamatan as id,".$sql_jumlah." kecamatan as nama from kecamatan kcm where kcm.id_kabupaten = '6405' order by kcm.kecamatan asc";
		}if($pilihan3 == 3){
			$sql_data="select um.id_umur as id,".$sql_jumlah." um.umur as nama, um.dari,um.sampai from umur um order by um.id_umur asc";
		}if($pilihan3 == 4){
			$sql_data="select j.id_jenis_peserta as id,".$sql_jumlah." jenis_peserta as nama from jenis_peserta j order by j.id_jenis_peserta asc";
		}else if($pilihan3 == 5){
			$sql_data="select ag.id_agama as id,".$sql_jumlah." ag.agama as nama from agama ag order by ag.id_agama asc";
		}else if($pilihan3 == 6){
			$sql_data="select jk.id as id,".$sql_jumlah." jk.nama as nama from jenis_kelamin jk order by jk.id asc";
		}
		
		//----------------- Tabel Grafik -----------------------------
		$qry_data=mysql_query($sql_data);
		//echo $sql_data;
		?>
		<table class="tabel" id="datatable" style="display: none;">
		<thead>
			<tr>
				<th></th>
				<?php
				foreach($arr_nama_kolom as $val_kolom){
					echo "<th>".$val_kolom."</th>";
				}
				?>
			</tr>
		</thead>
		<tbody>
		<?php
		$no=1;
		for($i=0;$i<count($arr_kolom);$i++){
			$jumlah_arr[$i]=0;
		}
		$total_semua=0;
		while($hasil_data=mysql_fetch_array($qry_data)){
		?>
			<tr>
				<td><?php echo $hasil_data['nama']; ?></td>
				<?php
				$subdata[$no]=0;
				foreach($arr_label as $key => $val){
					echo "<td align='center'>".$hasil_data[$val]."</td>";
					$subdata[$no]+=$hasil_data[$val];
					$jumlah_arr[$key]+=$hasil_data[$val];	
				}
				?>
			</tr>
		<?php
		}
		?>
		</tbody>
		</table>
		<!------------------------------------------------------------->
		<?php
			$qry_data=mysql_query($sql_data);
		?>
		<div id="dvData">
		<table class="tabel" id="dvData">
		<thead>
			<tr>
				<th>NO</th>
				<th>Nama</th>
				<?php
				foreach($arr_nama_kolom as $val_kolom){
					echo "<th>".$val_kolom."</th>";
				}
				?>
				<th>Total</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$no=1;
		for($i=0;$i<count($arr_kolom);$i++){
			$jumlah_arr[$i]=0;
		}
		$total_semua=0;
		while($hasil_data=mysql_fetch_array($qry_data)){
		?>
			<tr>
				<td align="center" width="30"><?php echo $no;?></td>
				<td><?php echo $hasil_data['nama']; ?></td>
				<?php
				$subdata[$no]=0;
				foreach($arr_label as $key => $val){
					echo "<td align='center'>".$hasil_data[$val]."</td>";
					$subdata[$no]+=$hasil_data[$val];
					$jumlah_arr[$key]+=$hasil_data[$val];	
				}
				$total_semua+=$subdata[$no];
				?>
				<td align="center" width="60"><?php echo $subdata[$no];?></td>
			</tr>
		<?php
			$no++;
		}
		?>
		<tr>
			<td colspan="2"><b>Total</b></td>
			<?php
			foreach($jumlah_arr as $val_jumlah){
				echo "<td align='center'><b>".$val_jumlah."</b></td>";
			}
			?>
			<td align="center"><b><?php echo $total_semua;?></b></td>
		</tr>
		</tbody>
		</table>
		</div>
		
	<?php
	}
?>